﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GoShopping.Model;

namespace GoShopping.BLL.SysInfo
{
    /// <summary>
    /// 评论操作类
    /// </summary>
    public class RemarkPrdctBean
    {
        public IList<RemarkPrdct> GetAllRemarkPrdct()
        {
            return Databases.DBGoShopping.Select(GoShoppingNBear.RemarkPrdct).ToList<RemarkPrdct>();
        }

        public IList<RemarkPrdct> GetRemarkPrdctList(string whereStr)
        {
            string sql = "SELECT * FROM " + GoShoppingNBear.RemarkPrdct.___GetTableName() + " WHERE 1=1 " + whereStr;
            return Databases.DBGoShopping.CustomSql(sql).ToList<RemarkPrdct>();
        }

        /// <summary>
        /// 添加一个评论
        /// </summary>
        /// <param name="adv"></param>
        /// <returns>返回添加的ID</returns>
        public int AddOneRemarkPrdct(RemarkPrdct adv)
        {
            return Databases.DBGoShopping.Insert(GoShoppingNBear.RemarkPrdct).AddColumn(
                GoShoppingNBear.RemarkPrdct.Content, adv.Content).AddColumn(
                GoShoppingNBear.RemarkPrdct.CreateDate, DateTime.Now).AddColumn(
                GoShoppingNBear.RemarkPrdct.IsCover, adv.IsCover).AddColumn(
                GoShoppingNBear.RemarkPrdct.IsRead, adv.IsRead).AddColumn(
                GoShoppingNBear.RemarkPrdct.ProductID, adv.ProductID).AddColumn(
                GoShoppingNBear.RemarkPrdct.ReplaceText, adv.ReplaceText).AddColumn(
                GoShoppingNBear.RemarkPrdct.ReplyDate, adv.ReplyDate).AddColumn(
                GoShoppingNBear.RemarkPrdct.ReplyerName, adv.ReplyerName).AddColumn(
                GoShoppingNBear.RemarkPrdct.Title, adv.Title).AddColumn(
                GoShoppingNBear.RemarkPrdct.UserID, adv.UserID).ExecuteReturnAutoIncrementID(
                GoShoppingNBear.RemarkPrdct.ID);
        }

        public void DelRemarkPrdct(int RemarkPrdctID)
        {
            Databases.DBGoShopping.Delete(GoShoppingNBear.RemarkPrdct).Where(
                GoShoppingNBear.RemarkPrdct.ID == RemarkPrdctID).Execute();
        }

        /// <summary>
        /// 回复一条评论
        /// </summary>
        /// <param name="RemarkPrdctID"></param>
        /// <param name="replaceText"></param>
        /// <param name="replyerName"></param>
        public void ReplyOneRemarkPrdct(int RemarkPrdctID, string replaceText, string replyerName)
        {
            Databases.DBGoShopping.Update(GoShoppingNBear.RemarkPrdct).Where(
                GoShoppingNBear.RemarkPrdct.ID == RemarkPrdctID).AddColumn(
                GoShoppingNBear.RemarkPrdct.IsRead, true).AddColumn(
                GoShoppingNBear.RemarkPrdct.ReplyDate, DateTime.Now).AddColumn(
                GoShoppingNBear.RemarkPrdct.ReplaceText, replaceText).AddColumn(
                GoShoppingNBear.RemarkPrdct.ReplyerName, replyerName).Execute();
        }

        /// <summary>
        /// 屏蔽/还原一条评论
        /// </summary>
        /// <param name="RemarkPrdctID"></param>
        /// <param name="covered"></param>
        public void CoverOneRemarkPrdct(int RemarkPrdctID, bool covered)
        {
            Databases.DBGoShopping.Update(GoShoppingNBear.RemarkPrdct).Where(
                GoShoppingNBear.RemarkPrdct.ID == RemarkPrdctID).AddColumn(
                GoShoppingNBear.RemarkPrdct.IsCover, covered).Execute();
        }

        /// <summary>
        /// 根据商品id查询该商品的评论
        /// </summary>
        /// <param name="productid"></param>
        /// <returns></returns>
        public IList<RemarkPrdct> GetRemarkByProduct(int productid)
        {
            return Databases.DBGoShopping.Select(
                GoShoppingNBear.RemarkPrdct).Where(
                GoShoppingNBear.RemarkPrdct.ProductID == productid).OrderBy(GoShoppingNBear.RemarkPrdct.ID.Desc).ToList<RemarkPrdct>();
        }

        /// <summary>
        /// 根据商品name查询该商品的评论
        /// </summary>
        /// <param name="productName"></param>
        /// <returns></returns>
        public IList<RemarkPrdct> GetRemarkByProduct(string productName)
        {
            ProductBean pb = new ProductBean();
            Product p = pb.GetProduct(productName);
            if (p == null)
            {
                throw new Exception(string.Format("找不到名称为{0}的商品", productName));
            }
            int productid = p.ID;
            return Databases.DBGoShopping.Select(
                GoShoppingNBear.RemarkPrdct).Where(
                GoShoppingNBear.RemarkPrdct.ProductID == productid).OrderBy(GoShoppingNBear.RemarkPrdct.ID.Desc).ToList<RemarkPrdct>();
        }

        /// <summary>
        /// 获取一个商品类型下评论的商品
        /// </summary>
        /// <param name="productTypeID"></param>
        public List<int> GetRemarkProductByType(int productTypeID)
        {
            List<CRemarkP> list = Databases.DBGoShopping.Select(
                GoShoppingNBear.RemarkPrdct, GoShoppingNBear.RemarkPrdct.ProductID.Distinct).Where(
                GoShoppingNBear.RemarkPrdct.ProductID.In(Databases.DBGoShopping.Select(GoShoppingNBear.Product, GoShoppingNBear.Product.ID).Where(GoShoppingNBear.Product.ProductTypeID == productTypeID).ToSubQuery()))
                .ToList<CRemarkP>();
            IEnumerable<int> result = from s in list
                                      select s.ProductID;
            List<int> rList = new List<int>();
            rList.AddRange(result);
            return rList;
        }
    }

    public class CRemarkP
    {
        private int _ProductID;

        public int ProductID
        {
            get { return _ProductID; }
            set { _ProductID = value; }
        }
    }
}
